<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.7.6/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="serial_num" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">微信昵称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="vx" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">订单状态</label>
                            <div class="layui-input-inline">
                                <select name="order_status" lay-verify="" lay-search="">
                                    <option value=""></option>
                                    <option value="0">未处理</option>
                                    <option value="1">待配送</option>
                                    <option value="2">已完成</option>
                                    <option value="3">退款</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">订单类型</label>
                            <div class="layui-input-inline">
                                <select name="order_type" lay-verify="" lay-search="">
                                    <option value=""></option>
                                    <option value="0">线下</option>
                                    <option value="1">美团</option>
                                    <option value="2">饿了么</option>
                                    <option value="3">抖音</option>
                                    <option value="4">高德</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">收货地址</label>
                            <div class="layui-input-inline">
                                <input type="text" name="address" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">配送时间范围</label>
                            <div class="layui-input-inline">
                                <input type="text" name="send_time_area" class="layui-input" id="test10" placeholder=" - ">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-primary layui-btn-sm data-edit-btn" lay-event="edit"> 编辑 </button>
<!--                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>-->
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script src="/static/lib/layui-v2.7.6/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            laydate = layui.laydate,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '/order/index',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50,fixed:'true',},
                {field: 'id', width: 60, title: 'ID', sort: true},
                {field: 'serial_num', width: 69, title: '编号',align:'center',sort: true,fixed:'true',edit: 'number'},
                {field: 'send_time', width: 170, title: '配送时间',align:'center', sort: true,fixed:'true', templet:function (d) {
                        if (d.start_time == d.end_time) {
                            return d.start_time;
                        } else {
                            return d.send_time;
                        }
                    }},
                {field: 'flower_name', width: 150, title: '花束名',align:'center',fixed:'true',edit: 'text'},
                {field: 'flower_img', width: 100, title: '花束图片',align:'center',fixed:'true'},
                {field: 'card', width: 69, title: '卡片',align:'center', sort: true,fixed:'true',
                    event: 'showCardContent', style:'cursor: pointer;', templet:function (d) {
                        if (d.card == 0) {
                            return `<i class="layui-icon" style="font-size: 20px; color: #dedada;">✖</i>`;
                        } else {
                            return `<i class="layui-icon" style="font-size: 20px; color: #51d021;">✔</i>` ;
                        }
                    }},
                {field: 'send_type', width: 93, title: '配送类型', align:'center',sort: true,fixed:'true', templet:function (d) {
                        if (d.send_type == 0) {
                            return `<span style="color: #ffbf18;">自配</span>`;
                        } else if (d.send_type == 1){
                            return `<span style="color: #f6092c;">达达</span>` ;
                        }else if (d.send_type == 2){
                            return `<span style="color: #51d021;">自取</span>` ;
                        }
                    }}, //0.自配 1.达达 2.自取
                {field: 'order_status', width: 93, title: '订单状态',align:'center', sort: true,fixed:'true', templet:function (d) {
                        if (d.order_status == 0) {
                            return `<span style="color: #f6092c;">未处理</span>`;
                        } else if (d.order_status == 1){
                            return `<span style="color: #ffbf18;">待配送</span>` ;
                        }else if (d.order_status == 2){
                            return `<span style="color: #51d021;">已完成</span>` ;
                        }else if (d.order_status == 3){
                            return `<span style="color: #adabab;">退款</span>` ;
                        }
                    }}, //0.未处理 1.待配送 2.已完成 3.退款
                {field: 'name', width: 93, title: '收花人姓名',align:'center',edit: 'text'},
                {field: 'mobile', width: 104, title: '收花人手机号',align:'center',edit: 'text'},
                {field: 'address', width: 200, title: '收花人地址',align:'center',edit: 'text'},
                {field: 'card_content', width: 100, title: '卡片内容',align:'center'},
                {field: 'vx', width: 100,title: '微信昵称',align:'center', edit: 'text'},
                {field: 'order_type', width: 93, title: '订单类型', align:'center',sort: true, templet:function (d) {
                        if (d.order_type == 0) {
                            return `<span style="color: #51d021;">线下</span>`;
                        } else if (d.order_type == 1){
                            return `<span style="color: #ffbf18;">美团</span>` ;
                        }else if (d.order_type == 2){
                            return `<span style="color: #3e8fea;">饿了么</span>` ;
                        }else if (d.order_type == 3){
                            return `<span style="color: #e59748;">抖音</span>` ;
                        }else if (d.order_type == 4){
                            return `<span style="color: #21d0d0;">高德</span>` ;
                        }
                    }}, //0.线下 1.美团 2.饿了么 3.抖音 4.高德
                {field: 'order_price', width: 93, title: '订单价格', align:'center',sort: true,edit: 'number'},
                {field: 'send_price', width: 93, title: '配送费', align:'center',sort: true,edit: 'number'},
                {field: 'offline_verify', width: 93, title: '线下核销',align:'center', sort: true, templet:function (d) {
                        if (d.offline_verify == 0) {
                            return `<span style="color: #3e8fea;">否</span>`;
                        } else if (d.offline_verify == 1){
                            return `<span style="color: #f6092c;">是</span>` ;
                        }
                    }}, //0.否 1是
                {title: '操作', minWidth: 130, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line',
            even: false, //开启隔行背景
            size: 'sm' //小尺寸的表格
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加订单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../page/order/add.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'edit') {
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var index = layer.open({
                    title: '编辑订单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../page/order/edit.html?id='+data[0].id,
                    // success: function (layero,index) {
                    //     //表单初始赋值
                    //    var body = layer.getChildFrame('body',index);
                    //     var iframeWin = layero.find('iframe')[0].contentWindow;
                    //     body.find("input[name='id']").val(data.id)
                    //     iframeWin.layui.form.render();
                    // }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            console.log(data.id)
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑订单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../page/order/edit.html?id='+data.id,
                    // success: function (layero,index) {
                    //     //表单初始赋值
                    //    var body = layer.getChildFrame('body',index);
                    //     var iframeWin = layero.find('iframe')[0].contentWindow;
                    //     body.find("input[name='id']").val(data.id)
                    //     iframeWin.layui.form.render();
                    // }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'showCardContent'){
                if (data.card == 0){
                    return false
                }
                layer.prompt({
                    formType: 2
                    ,title: ' 编号['+ data.serial_num +'] 的卡片内容'
                    ,value: data.card_content
                }, function(value, index){
                    layer.close(index);

                    //这里一般是发送修改的Ajax请求
                    $.ajax({
                        url: "/order/edit",
                        type: 'post',
                        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                        data: JSON.stringify({id:data.id,card_content: value}),
                        async: false,
                        success: function (ret) {
                            layer.msg("操作成功");
                        },
                        error: function (xhr, textstatus, thrown) {
                            layer.msg('操作失败');
                        }
                    });
                    //同步更新表格和缓存对应的值
                    obj.update({
                        card_content: value
                    });
                });
            } else if(obj.event === 'changeSendTime'){
                layer.prompt({
                    formType: 2
                    ,title: '修改 ID 为 ['+ data.id +'] 的用户签名'
                    ,value: data.send_time
                }, function(value, index){
                    layer.close(index);

                    //这里一般是发送修改的Ajax请求

                    //同步更新表格和缓存对应的值
                    obj.update({
                        send_time: value
                    });
                });
            }
        });
        //监听单元格编辑
        table.on('edit(currentTableFilter)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
            $.ajax({
                url: "/order/edit",
                type: 'post',
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                data: JSON.stringify(data),
                async: false,
                success: function (ret) {
                    // var msg = ret.msg || "操作成功"
                    layer.msg("操作成功");
                },
                error: function (xhr, textstatus, thrown) {
                    layer.msg('操作失败');
                }
            });
        });
        //日期时间范围
        laydate.render({
            elem: '#test10'
            ,type: 'datetime'
            ,range: true
        });
    });
</script>

</body>
</html>